package com.tnsw.facebookautoliker;

import com.tnsw.coreutils.MyLog;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ListViewDragExtender implements OnTouchListener, OnScrollListener {
	
	protected String tag = "ListViewDragExtender";
	protected Context context;
	protected ListView parentLV;
	protected BaseAdapter baseApater;
	
	protected LinearLayout layoutUpdating;
	private int oldX, oldY;
	private boolean _isUpdating = false;
	private boolean _isDragging = false;
	private boolean isJustStart = false;
	private boolean _isPulling = false;
	
	protected ListViewDragInterface intf;
	protected StandardUpdateView updateView;
	protected int scrollState = SCROLL_STATE_IDLE;
	
	protected boolean _isEnable = true;
	
	public ListViewDragExtender(Context context, ListView parentLV, LinearLayout layoutUpdating, BaseAdapter baseAdapter) {
		this.context = context;
		this.parentLV = parentLV;
		this.layoutUpdating = layoutUpdating;
		this.baseApater = baseAdapter;
		
		this.parentLV.setOnTouchListener(this);
		this.parentLV.setOnScrollListener(this);
		
		this.layoutUpdating.setVisibility(View.GONE);
	
	}
	
	public void setEnable(boolean b) {
		this._isEnable = b;
	}
	
	public boolean isEnable() {
		return(this._isEnable);
	}
	
	public void setUpdateView(StandardUpdateView updateView) {
		this.updateView = updateView;
		this.updateView.setDragExtender(this);
	}
	
	public void setListViewDragInterface(ListViewDragInterface intf) {
		this.intf = intf;
	}
	
	public boolean isUpdating() {
		return(this._isUpdating);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (this._isEnable == false) {
			return(false);
		}
		
		int x = (int) event.getRawX();
		int y = (int) event.getRawY();
		
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				this.oldX = x;
				this.oldY = y;
				
				//return(true);
				
				if ( (this.scrollState == SCROLL_STATE_FLING) || (this.scrollState == SCROLL_STATE_TOUCH_SCROLL) ) {
					return(false);	
				} else {
					return(true);
				}
				
				//break;
				
			case MotionEvent.ACTION_MOVE:
				this._isDragging = true;
				
				if (this.updateView != null) {
					if (this.updateView.getCurrentStage() == 2) return(false);
				}
				
				int deltaY = y - this.oldY;
				
				if (this.parentLV.getVisibility() != View.GONE) {
					int topY = this.parentLV.getChildAt(0).getTop();
					if (this.parentLV.getFirstVisiblePosition() == 0) {
						if (deltaY >= 0) {
							if (topY >= 0) {
								if (this.isJustStart == false) {
									this.isJustStart = true;
									this.oldY = y-1;
									deltaY = 1;
								}
								
								if (this.layoutUpdating.getVisibility() == View.GONE) {
									this.layoutUpdating.setVisibility(View.VISIBLE);
								}
								
								this.layoutUpdating.getLayoutParams().height = deltaY;
								this.layoutUpdating.requestLayout();
																
								this._isPulling = true;
								if (this.intf != null) {
									this.intf.onPulling(deltaY);
								}
								
							} else {
								this.layoutUpdating.getLayoutParams().height = 0;
								this.layoutUpdating.requestLayout();
								return(false);
							}
							
							return(true);
							
						} else {
							if (topY <= 0) {
								if (this.isUpdating() == false) {
									this.layoutUpdating.getLayoutParams().height = 0;
									this.layoutUpdating.requestLayout();
								}
							}
						}
						
						return(false);
					} else {
						return(false);
					}
				}
				
				break;
			
			case MotionEvent.ACTION_UP:
				
				this.oldX = -1;
				this.oldY = -1;
				
				this.isJustStart = false;
				boolean bb = false;
				
				if (this.layoutUpdating.getVisibility() == View.VISIBLE) {
					if (this._isPulling == true) {
						this.baseApater.notifyDataSetChanged();
						
					}
				}
				
				if (this.layoutUpdating.getVisibility() == View.VISIBLE) {
					this._isPulling = false;
					if (this.intf != null) {
						this.intf.onRelease();
					}
				}
				
				this._isDragging = false;
				return(bb);
		}
		
		return true;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
		
		switch (scrollState) {
			case SCROLL_STATE_FLING:
				break;
				
			case SCROLL_STATE_IDLE:
				break;
				
			case SCROLL_STATE_TOUCH_SCROLL:
				break;
		}
	}
	
}
